package com.zy.io;

import java.io.*;
import java.nio.charset.StandardCharsets;

/**
 * @Author Gong
 * @Description
 * @dATE 2021/12/12 14:56
 **/
public class RandomAcessFileTest {
    public static void main(String[] args) throws IOException {
        insert("d:/temp/test.txt",3,"插入指定位置指定内容");

    }
    public static void insert(String filePath,long pos,String insertContent)throws IOException{
        RandomAccessFile raf=null;
        File tmp=File.createTempFile("tmp",null);
        tmp.deleteOnExit();
        try {
            // 以读写的方式打开一个RandomAccessFile对象
            raf = new RandomAccessFile(new File(filePath), "rw");
            //创建一个临时文件来保存插入点后的数据
            FileOutputStream fileOutputStream = new FileOutputStream(tmp);
            FileInputStream fileInputStream = new FileInputStream(tmp);
            //把文件记录指针定位到pos位置
            raf.seek(pos);
            raf.seek(pos);
            //------下面代码将插入点后的内容读入临时文件中保存-----
            byte[] bbuf = new byte[64];
            //用于保存实际读取的字节数据
            int hasRead = 0;
            //使用循环读取插入点后的数据
            while ((hasRead = raf.read(bbuf)) != -1) {
                //将读取的内容写入临时文件
                fileOutputStream.write(bbuf, 0, hasRead);
            }
            //-----下面代码用于插入内容 -----
            //把文件记录指针重新定位到pos位置
            raf.seek(pos);
            //追加需要插入的内容
            raf.write(insertContent.getBytes());
            //追加临时文件中的内容
            while ((hasRead = fileInputStream.read(bbuf)) != -1) {
                //将读取的内容写入临时文件
                raf.write(bbuf, 0, hasRead);
            }
        }catch (Exception e){
            throw  e;
        }
    }


    public  static  void appendTest() throws IOException {
        String filePath="d:/temp/test.txt";
        RandomAccessFile raf=null;
        File file=null;
        try {
            file=new File(filePath);
            // 以读写的方式打开一个RandomAccessFile对象
            raf=new RandomAccessFile(file,"rw");
            //将记录指针移动到该文件的最后
            raf.seek(raf.length());
            //向文件末尾追加内容
            raf.writeChars("abc...");
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            raf.close();
        }

    }
    public  static  void readTest() throws IOException {
        String filePath="D:\\workspace_python\\pachong\\novel";
        RandomAccessFile raf=null;
        File file=null;
        try {
            file=new File(filePath);
            raf=new RandomAccessFile(file,"r");
            // 获取 RandomAccessFile对象文件指针的位置，初始位置为0
            System.out.print("输入内容："+raf.getFilePointer());
            //移动文件记录指针的位置
            raf.seek(1000);

            byte[] b=new byte[1024];
            int hasRead=0;
            //循环读取文件
            while((hasRead=raf.read(b))>0){
                //输出文件读取的内容
                System.out.print(new String(b,0,hasRead));
            }
        }catch (IOException e){
            e.printStackTrace();
        }finally {
            raf.close();
        }

    }
    public static void write() throws IOException {
        RandomAccessFile raf = new RandomAccessFile("d:/temp/test.txt", "rw");

        raf.seek(3);

//先读出来
        String temp = raf.readLine();

        raf.seek(3);
        raf.write("xyz".getBytes());
        raf.write(temp.getBytes());

        raf.close();

    }
    public static void read() throws IOException {
        RandomAccessFile raf = new RandomAccessFile("d:/temp/test.txt", "rw");


        byte [] b = new byte[1024];
        for (int i = 0; i < 5; i++) {
            raf.seek(5);
            int off = 0;
            int len = 5;
            raf.read(b, off, len);
            String str = new String(b, 0, len);
            System.out.println(str);
        }




        raf.close();
    }

}
